package com.acwing.www;

import java.util.Arrays;

/**
 * Creared with IntelliJ IDEA.
 * Description:冒泡排序 时间复杂度O(N^2) 空间复杂度O(1) 稳定性 稳定
 * User:yxd
 * Date:2022-01-24
 * Time:19:42
 */
public class Paopao {
    public static void main(String[] args) {
        int[] array = {2,4,1,23,15,24,55,14};
        long start = System.currentTimeMillis();//开始时间
        bubbleSort(array);
        long end = System.currentTimeMillis();//结束时间
        System.out.println(end - start);
        System.out.println(Arrays.toString(array));
    }
    public static void bubbleSort(int[] array){
        for (int i = 0; i < array.length - 1; i++) {
            boolean flg = false;
            for (int j = 0; j < array.length - 1 - i; j++) {
                if(array[j + 1] < array[j]){
                    int tmp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = tmp;
                    flg = true;
                }
            }
            if(!flg){
                break;
            }
        }
    }
}
